
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// </editor-fold>

public class ContextInit implements ServletContextListener {

// <editor-fold defaultstate="collapsed" desc="init variables">
    public static final Logger log = LoggerFactory.getLogger(ContextInit.class);

    public static final String VERSION = "ContextInit v2.2 2008-04-02";

    public static Map<String,Clickstream> clientList;

    private ServletContext context;
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="contextInitialized()">

    @Override
    public void contextInitialized(ServletContextEvent event) {

        StringBuilder out = new StringBuilder();
        out.append("Init: ").append(VERSION);

        clientList = new HashMap<String,Clickstream>();

        context = event.getServletContext();
        try {
            // maps all session
            context.setAttribute("context.clientList", clientList);
            out.append("\tcontext.clientList ... Done\n");

            // init sqlbean
            MySQL sb = new MySQL();
            context.setAttribute("sb", sb);
            out.append("\tMysql=").append(sb.getVersion()).append(" sb ... Done\n");

            // init mail
            Mail mail = new Mail();
            context.setAttribute("mail", mail);
            out.append("\tmail ... Done\n");

            // init
            AdvertArray advertArray = new AdvertArray(context, sb);
            advertArray.fillArray();
            context.setAttribute("advertArray", advertArray);
            out.append("\tadvertArray ... Done");

        }
        catch(Exception ne) {
            log.error("ContextInit init error:\n" + ne);
        }

        log.info(out.toString());
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="contextDestroyed()">

    @Override
    public void contextDestroyed(ServletContextEvent event) {

        context = event.getServletContext();
        context.removeAttribute("context.clientList");
        context.removeAttribute("sb");
        context.removeAttribute("mail");
        context.removeAttribute("advertArray");

        this.context = null;

        log.info("Destroy: " + VERSION);
    }
// </editor-fold>

}
